/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.safetytask.mapper;

import com.koron.safetytask.domain.JobsTasks;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface JobsTasksMapper {

	@Insert("insert into jobs_tasks(id,add_time,begin_time,end_time,completed_time,staff_code,staff_name,state,fstatusflag,note,task_type,task_label,task_object,task_object_id,task_desc,source_id,npc_code,business_id)"
			+ "values(#{id},#{addTime},#{beginTime},#{endTime},#{completedTime},#{staffCode},#{staffName},#{state},#{fstatusflag},#{note},#{taskType},#{taskLabel},#{taskObject},#{taskObjectId},#{taskDesc},#{sourceId},#{npcCode},#{businessId})")
	int insert(JobsTasks bean);

	@Update("update jobs_tasks set npc_code=#{npcCode},note=#{note},staff_code=#{staffCode},staff_name=#{staffName} where id=#{id}")
	int update(JobsTasks bean);

	@Update("update jobs_tasks set state=#{state},fstatusflag=#{fstatusflag},note=#{note},completed_time=#{completedTime},business_id=#{businessId} where id=#{id}")
	int completed(JobsTasks bean);

	@Delete("delete from jobs_tasks where id=#{id}")
	int delete(@Param("id") String id);

	@Select("select * from jobs_tasks where id=#{id}")
	JobsTasks queryById(@Param("id") String id);

	@Select("<script>"
			+ "select * from jobs_tasks where 1=1"
			+ "<if test='staffCode!=null'> and staff_code = #{staffCode}</if>"
			+ "<if test='staffName!=null'> and staff_name like CONCAT(CONCAT('%',#{staffName}),'%')</if>"
			+ "<if test='state!=null'> and state = #{state}</if>"
			+ "<if test='begin!=null'> and begin_time &gt;= #{begin}</if>"
			+ "<if test='end!=null'> and end_time &lt;= #{end}</if>"
			+ "<if test='typeIdList!=null'> and task_type in "
			+ "<foreach collection='typeIdList' open='(' close=')' separator=',' item='typeId' > #{typeId} "
			+ "</foreach></if>"
			+ " order by begin_time desc"
			+ "<if test='start!=null'> limit #{start},#{row}</if>"
			+ "</script>")
	List<JobsTasks> queryByStaff(@Param("staffCode") String staffCode,@Param("staffName") String staffName,@Param("typeIdList") List<String> typeIdList,
			@Param("state") Integer state,@Param("begin") Date begin,	@Param("end") Date end,@Param("start") Integer start,@Param("row") Integer row);

	@Select("<script>"
			+ "select count(*) from jobs_tasks where 1=1"
			+ "<if test='staffCode!=null'> and staff_code = #{staffCode}</if>"
			+ "<if test='staffName!=null'> and staff_name like CONCAT(CONCAT('%',#{staffName}),'%')</if>"
			+ "<if test='state!=null'> and state = #{state}</if>"
			+ "<if test='begin!=null'> and begin_time &gt;= #{begin}</if>"
			+ "<if test='end!=null'> and end_time &lt;= #{end}</if>"
			+ "<if test='typeIdList!=null'> and task_type in "
			+ "<foreach collection='typeIdList' open='(' close=')' separator=',' item='typeId' > #{typeId} "
			+ "</foreach></if>"
			+ "</script>")
	int count(@Param("staffCode") String staffCode,@Param("staffName") String staffName,@Param("typeIdList") List<String> typeIdList,@Param("state") Integer state,
			@Param("begin") Date begin,@Param("end") Date end);

	@Select("select count(*) from jobs_tasks where begin_time=#{beginDate} and  source_id=#{sourceId}")
	int check(@Param("sourceId") String sourceId,@Param("beginDate") String beginDate);

	@Select("select work_type_id from work_list_type where router=#{router}")
	List<String> typeListByRouter(@Param("router")String router);
	
	/**
	 * 根据业务主键id，获取任务
	 * @param id
	 * @return
	 */
	@Select("select * from jobs_tasks where business_id=#{businessId}")
	JobsTasks queryJobsTasksByBusinId(@Param("businessId") String id);

	@Select("select * from jobs_tasks where id=#{id}")
	JobsTasks getOne(@Param("id") String id);
}
